//index.js
//获取应用实例
const app = getApp()

Page({
	data: {
		src: '',
		input_wrds: '',
	},
	//清空文字
	input_click: function() {
		this.setData({
			input_wrds: ' '
		})
	},
	onLoad: function(options) {
		var nickName = wx.getStorageSync('nickName');
		var type = options.type
		console.log(type)
		var type = type.split(","); //转成
		type.splice(-1, 1) //删除，
		//声明
		let num = 0;
		let words = '';
		let img = '';
		let cover = '';
		let type_1_1 = '';
		let type_1_2 = '';
		let type_2_1 = '';
		let type_2_2 = '';
		let type_3_1 = '';
		let type_3_2 = '';
		let type_4_1 = '';
		let type_4_2 = '';
		let type_5_1 = '';
		let type_5_2 = '';
		let type_6_1 = '';
		let type_6_2 = '';
		let type_7_1 = '';
		let type_7_2 = '';
		let type_8_1 = '';
		let type_8_2 = '';
		let type_9_1 = '';
		let type_9_2 = '';
		let type_10_1 = '';
		let type_10_2 = '';
		let img_src = '';
		let ol_wrds = '';
		let fruits = [{
				'img': 'sg_4',
				'url': '218'
			},
			{
				'img': 'sg_2',
				'url': '157'
			},
			{
				'img': 'sg_6',
				'url': '163'
			},
			{
				'img': 'sg_10',
				'url': '154'
			},
			{
				'img': 'sg_8',
				'url': '162'
			},
			{
				'img': 'sg_15',
				'url': '184'
			},
			{
				'img': 'sg_4',
				'url': '218'
			},
			{
				'img': 'sg_14',
				'url': '155'
			},
		];
		let wrds_wrds = [{
				'wrds': '心理',
				'url': ''
			},
			{
				'wrds': '久坐',
				'url': ''
			},
			{
				'wrds': '抽烟',
				'url': ''
			},
			{
				'wrds': '喝酒',
				'url': ''
			},
			{
				'wrds': '睡眠',
				'url': ''
			},
			{
				'wrds': '运动',
				'url': ''
			},
			{
				'wrds': '饮食',
				'url': ''
			},
			{
				'wrds': '低头族',
				'url': ''
			},
		]
		let wrds_data = [];
		let icon_data = [];
		//
		for (var i = 0; i < type.length; i++) {
			if (type[i] == 1) {
				num += 0;
			} else if (type[i] == 2) {
				num += 0.5;
			} else {
				num += 1;
			};
			//
			if (type[i] > 1) {
				wrds_data.push(wrds_wrds[i]);
			};
			//
			icon_data.push((i + 1) + '_' + type[i]);
		};
		//文字
		if (num == 0) {
			words = '您的身体倍儿棒，继续保持良好的生活习惯，保持积极乐观心态、疾病会离你远远滴！';
			cover = 'cover_1.png';
			img_src = 'bg_7.png';
			wrds_data.push({
				'wrds': '棒棒哒',
				'url': ''
			});
		} else if (num > 0 && num <= 2) {
			words = '莫大意，您的身体可能有轻度的健康问题，适量的运动加上良好的生活习惯可以让您恢复健康状态。';
			cover = 'cover_2.png';
			img_src = 'bg_8.png';
		} else if (num > 2 && num <= 4) {
			words = '黄色预警信号启动，加强运动，注意饮食，放松心态，马上来个说走就走的旅行！';
			cover = 'cover_2.png';
			img_src = 'bg_9.png';
		} else if (num > 4 && num <= 6) {
			words = '红色预警信号启动，您需要以健康的名义狠狠地爱自己一把！生活习惯急需改正。';
			cover = 'cover_3.png';
			img_src = 'bg_10.png';
		} else if (num > 6) {
			words = '可怜的娃……生活虽然不易但也不带这么折磨自己的！强烈建议，赶紧就医做个检查，改变自己的生活习惯。';
			cover = 'cover_3.png';
			img_src = 'bg_11.png';
		};
		this.data.cover = cover;
		this.data.words = words;
		this.data.src = img_src;
		this.data.icon = icon_data;
		//图片 和水果
		if (num > 0) {
			fruits = []
		};
		if (type[0] >= 2) {
			if (type[0] == 2) {
				type_9_1 += 'type_1'
			} else {
				type_9_2 += 'type_2'
			}
			fruits.push({
				'img': 'sg_1',
				'url': '164'
			})
		}
		if (type[1] >= 2) {
			if (type[1] == 2) {
				type_5_1 += 'type_1';
			} else {
				type_5_1 += 'type_2';
			}
			fruits.push({
				'img': 'sg_4',
				'url': '218'
			}, {
				'img': 'sg_7',
				'url': '165'
			})
		}
		if (type[2] >= 2) {
			if (type[2] == 2) {
				type_2_1 += 'type_1';
				type_7_1 += 'type_1';
			} else {
				type_2_1 += 'type_2';
				type_7_1 += 'type_2';
			}
			fruits.push({
				'img': 'sg_9',
				'url': '156'
			}, {
				'img': 'sg_6',
				'url': '160'
			})
		}
		if (type[3] >= 2) {
			if (type[3] == 2) {
				type_3_1 += 'type_1';
				type_6_1 += 'type_1';
				type_10_1 += 'type_1';
			} else {
				type_3_1 += 'type_2';
				type_6_1 += 'type_2';
				type_10_1 += 'type_2';
			}
			fruits.push({
				'img': 'sg_13',
				'url': '171'
			}, {
				'img': 'sg_3',
				'url': '158'
			}, {
				'img': 'sg_11',
				'url': '169'
			})
		}
		if (type[4] >= 2) {
			if (type[4] == 2) {
				type_1_1 += 'type_1';
				type_3_1 += 'type_1';
			} else {
				type_1_1 += 'type_2';
				type_3_1 += 'type_2';
			}
			fruits.push({
				'img': 'sg_8',
				'url': '163'
			}, {
				'img': 'sg_10',
				'url': '154'
			}, {
				'img': 'sg_14',
				'url': '155'
			})
		}
		if (type[5] >= 2) {
			if (type[5] == 2) {
				type_1_1 += 'type_1';
				type_6_1 += 'type_1';
			} else {
				type_1_1 += 'type_2';
				type_6_1 += 'type_2';
			}
		}
		if (type[6] >= 2) {
			if (type[6] == 2) {
				type_4_1 += 'type_1';
				type_10_1 += 'type_1';
			} else {
				type_4_1 += 'type_2';
				type_10_1 += 'type_2';
			}
			fruits.push({
				'img': 'sg_12',
				'url': '159'
			}, {
				'img': 'sg_2',
				'url': '157'
			})
		}
		if (type[7] >= 2) {
			if (type[7] == 2) {
				type_6_1 += 'type_1';
				type_8_1 += 'type_1';
			} else {
				type_6_1 += 'type_2';
				type_8_1 += 'type_2';
			}
			fruits.push({
				'img': 'sg_15',
				'url': '184'
			}, {
				'img': 'sg_5',
				'url': '173'
			})
		};
		//设置宽
		this.setData({
			width: (num + 1) * (100 / 9) + '%',
			banner_img: cover,
			banner_words: words,
			data_type_1_1: type_1_1,
			data_type_1_2: type_1_2,
			data_type_2_1: type_2_1,
			data_type_2_2: type_2_2,
			data_type_3_1: type_3_1,
			data_type_3_2: type_3_2,
			data_type_4_1: type_4_1,
			data_type_4_2: type_4_2,
			data_type_5_1: type_5_1,
			data_type_5_2: type_5_2,
			data_type_6_1: type_6_1,
			data_type_6_2: type_6_2,
			data_type_7_1: type_7_1,
			data_type_7_2: type_7_2,
			data_type_8_1: type_8_1,
			data_type_8_2: type_8_2,
			data_type_9_1: type_9_1,
			data_type_9_2: type_9_2,
			data_type_10_1: type_10_1,
			data_type_10_2: type_10_2,
			fruits_list: fruits,
			input_name: nickName,
			img_num: this.data.src,
			wrds_list: wrds_data,
		});


	},
	onShareAppMessage: function() {
		return {
			title: '「趣味画像」让健康更简单',
			imageUrl: 'https://resource.160nurse.com/upload/miniprogram/index.jpeg'
		}
	},
	handleGetMessage: function(e) {
		wx.downloadFile({
			url: e.target.data,
			success: function(res) {
				console.log(res)
				wx.saveImageToPhotosAlbum({
					filePath: res.tempFilePath,
					success: function(res) {
						console.log(res)
					},
					fail: function(res) {
						console.log(res)
						console.log('fail')
					}
				})
			},
			fail: function() {
				console.log('fail')
			}
		})
	},
	searchBox: function(e) {
		if (e.detail.value.input) {
			this.data.input = e.detail.value.input;
		} else {
			this.data.input = '健康快乐每一天'
		}

		wx.showToast({
			title: '保存中',
			icon: 'loading',
			duration: 10000,
			success: function() {
				console.log(1)
			}
		});
		//
		var width = '';
		var height = '';
		var canvasimgbg = '';
		//
		wx.getSystemInfo({
			success: function(res) {
				width = res.windowWidth;
			}
		})
		var context = wx.createCanvasContext('share')
		context.width = width;
		context.height = width * 2.3;
		context.setFillStyle('#fff')
		context.fillRect(0, 0, width, width * 2.3)
		// context.stroke()
		context.drawImage('../../PuBlic/img/bg_4.png', 0, 0, width, width * 0.463);
		context.drawImage('../../PuBlic/img/bg_5.png', width * 0.225, width * 0.05, width * 0.55, width * 0.18);
		context.drawImage('../../PuBlic/img/' + this.data.src, 0, width * 1.7, width, width * 0.15625);
		context.drawImage('../../PuBlic/img/' + this.data.cover, width / 4, width * 0.25, width / 2, width / 2);
		context.drawImage('../../PuBlic/img/ewm.png', width * 0.2, width * 1.84, width * 0.6, width * 0.460);
		//iocn 循环
		for (let i = 0; i < 4; i++) {
			context.drawImage('../../PuBlic/icon/' + this.data.icon[i] + '.png', width * (0.018 + ((i + 1) / 30) + (i * 0.20)),
				width * 1.24, width * 0.20, width * 0.20);
		}
		for (let i = 4; i < 8; i++) {
			context.drawImage('../../PuBlic/icon/' + this.data.icon[i] + '.png', width * (0.018 + ((i - 3) / 30) + ((i - 4) *
				0.20)), width * 1.47, width * 0.20, width * 0.20);
		}
		context.drawImage('../../PuBlic/img/an_1.png', width * 0.4, width * 1.1, width * 0.2, width * 0.091);
		//
		var text = this.data.words; //这是要绘制的文本
		var chr = text.split(""); //这个方法是将一个字符串分割成字符串数组
		var temp = "";
		var row = [];
		context.setFontSize(14)
		context.setFillStyle("#878787")
		for (var a = 0; a < chr.length; a++) {
			if (context.measureText(temp).width < width * 0.8) {
				temp += chr[a];
			} else {
				a--; //这里添加了a-- 是为了防止字符丢失，效果图中有对比
				row.push(temp);
				temp = "";
			}
		}
		row.push(temp);
		for (var b = 0; b < row.length; b++) {
			context.fillText(row[b], width * 0.14, width * 0.85 + b * 30);
		}
		context.draw(false, this.getTempFilePath)
	},
	//获取临时路径
	getTempFilePath: function() {
		wx.canvasToTempFilePath({
			canvasId: 'share',
			success: (res) => {
				this.setData({
					shareTempFilePath: res.tempFilePath
				})
				//保存至相册
				wx.hideToast()
				wx.saveImageToPhotosAlbum({
					filePath: this.data.shareTempFilePath,
					success: (res) => {
						wx.showModal({
							title: '提示',
							content: '保存成功',
							showCancel: true
						})
					},
					fail: (err) => {
						console.log(err)
					}
				});
			}
		})
	},

})
